﻿using System;
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Input;

namespace TCP232.Views.UserControls
{
    public class Hyperlink : TextBlock, ICommandSource
    {
        //private string _url;
        //public static readonly DirectProperty<Hyperlink, string> UrlProperty
        //    = AvaloniaProperty.RegisterDirect<Hyperlink, string>(nameof(Url), o => o.Url, (o, v) => o.Url = v);
        //public string Url
        //{
        //    get => _url;
        //    set => SetAndRaise(UrlProperty, ref _url, value);
        //}

        private ICommand _command;
        public ICommand Command
        {
            get => _command;
            set => SetAndRaise(CommandProperty, ref _command, value);
        }

        public static readonly DirectProperty<Hyperlink, ICommand> CommandProperty =
            AvaloniaProperty.RegisterDirect<Hyperlink, ICommand>(nameof(Command), o => o.Command, (o, v) => o.Command = v);

        //public static readonly DirectProperty<Hyperlink, ICommand> CommandProperty =
        //    AvaloniaProperty.RegisterDirect(nameof(Command), (Hyperlink hyperlink) => hyperlink.Command, delegate (Hyperlink hyperlink, ICommand command)
        //{
        //    hyperlink.Command = command;
        //}, null, BindingMode.TwoWay, enableDataValidation: true);

        public object CommandParameter
        {
            get => GetValue(CommandParameterProperty);
            set => SetValue(CommandParameterProperty, value);
        }

        public static readonly StyledProperty<object> CommandParameterProperty = AvaloniaProperty.Register<Hyperlink, object>(nameof(CommandParameter));

        protected override void OnPointerPressed(PointerPressedEventArgs e)
        {
            base.OnPointerPressed(e);
            if (Command != null && Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }

            //new Button().Command =;
        }

        public void CanExecuteChanged(object sender, EventArgs e)
        {
            if (Command != null && Command.CanExecute(CommandParameter))
            {
                UpdateIsEffectivelyEnabled();
            }
            //bool flag = Command == null || Command.CanExecute(CommandParameter);
            //if (flag != _commandCanExecute)
            //{
            //    _commandCanExecute = flag;

            //}
        }
    }
}